package com.ithiema.EmployeeListener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.ithiema.dao.EmployeeDao;
import com.ithiema.pojo.Employee;

import java.util.ArrayList;
import java.util.List;

public class EmployeeListener implements ReadListener<Employee> {
    private int count = 100;
    private EmployeeDao dao ;
    private List<Employee> list = new ArrayList<>(count);

    public EmployeeListener(EmployeeDao dao) {
        this.dao = dao;
    }
    int c = 0;
    @Override
    public void invoke(Employee employee, AnalysisContext analysisContext) {
        list.add(employee);
        if(list.size()>=count){
            dao.saveData(list);
            list = new ArrayList<>(count);
            c++;
            if(c%100==0){
                System.gc();
            }
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if(list.size()>0){
            dao.saveData(list);
        }
    }
}
